# 数据库迁移脚本
param(
    [string]$Action = "update",  # update, add, remove
    [string]$MigrationName = ""
)

$ProjectPath = "src\TerritoryGame.Infrastructure"
$StartupProjectPath = "src\TerritoryGame.API"

Write-Host "🎯 涂色抢地盘游戏 - 数据库迁移工具" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan

switch ($Action.ToLower()) {
    "add" {
        if ([string]::IsNullOrEmpty($MigrationName)) {
            Write-Host "❌ 请提供迁移名称" -ForegroundColor Red
            Write-Host "用法: .\migrate-database.ps1 -Action add -MigrationName 'YourMigrationName'" -ForegroundColor Yellow
            exit 1
        }
        
        Write-Host "📝 创建新的数据库迁移: $MigrationName" -ForegroundColor Yellow
        dotnet ef migrations add $MigrationName -p $ProjectPath -s $StartupProjectPath
    }
    
    "update" {
        Write-Host "🔄 更新数据库到最新迁移" -ForegroundColor Yellow
        dotnet ef database update -p $ProjectPath -s $StartupProjectPath
    }
    
    "remove" {
        Write-Host "🗑️ 移除最后一个迁移" -ForegroundColor Yellow
        dotnet ef migrations remove -p $ProjectPath -s $StartupProjectPath
    }
    
    "list" {
        Write-Host "📋 列出所有迁移" -ForegroundColor Yellow
        dotnet ef migrations list -p $ProjectPath -s $StartupProjectPath
    }
    
    "seed" {
        Write-Host "🌱 运行种子数据" -ForegroundColor Yellow
        Write-Host "启动应用程序以自动运行种子数据..." -ForegroundColor Green
        dotnet run --project $StartupProjectPath
    }
    
    default {
        Write-Host "❌ 未知的操作: $Action" -ForegroundColor Red
        Write-Host "可用操作:" -ForegroundColor Yellow
        Write-Host "  - add: 添加新迁移" -ForegroundColor White
        Write-Host "  - update: 更新数据库" -ForegroundColor White
        Write-Host "  - remove: 移除最后迁移" -ForegroundColor White
        Write-Host "  - list: 列出所有迁移" -ForegroundColor White
        Write-Host "  - seed: 运行种子数据" -ForegroundColor White
        Write-Host ""
        Write-Host "示例:" -ForegroundColor Cyan
        Write-Host "  .\migrate-database.ps1 -Action update" -ForegroundColor White
        Write-Host "  .\migrate-database.ps1 -Action add -MigrationName 'AddUserTable'" -ForegroundColor White
    }
}

Write-Host "✅ 操作完成!" -ForegroundColor Green
